﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HighPerformanceLog;
using HighPerformanceLog.V2;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using ProcessBarDemo.UpdateFramework;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using test.Views;
using test.Views.SettingControl;
using XMLConfig;
using Ookii.Dialogs.Wpf;



namespace test.ViewModels
{
    public partial class MainViewModel : ObservableObject
    {
        [ObservableProperty]
        private string infoMessage = "东东电竞全民争霸最新公告 …… 07/03";
        [ObservableProperty]
        private double progressValue = 0;
        [ObservableProperty]
        private bool isComponentVisible = true;
        [ObservableProperty]
        private bool isLoading = false;
        [ObservableProperty]
        private bool xxXXxxx = false;
        //退出选择项
        [ObservableProperty]
        private bool isOption1Selected = false;

        [ObservableProperty]
        private bool isOption2Selected = true;
        //退出界面使用的
        [ObservableProperty]
        private bool quiTeyes = true;
        [ObservableProperty]
        private bool quiTeno = false;
        //是否开机自启动
        [ObservableProperty]
        private bool autotoStart ;
        //是否选择自动更新
        [ObservableProperty]
        private bool autoUpdate;
        public Action? CloseWindowAction { get; set; }
        private UpdateManager updater = new UpdateManager();
        private string xmlPath = Path.Combine("..", "..", "..", "config", "update_files.xml");
        //下载地址
        [ObservableProperty]
        private string baseDirectory;
        //下载路径修改设置
        
        [RelayCommand]
        private void Choosefile()
        {
          
            string selectedDir;
            bool success = DialogUtil.GetDir("请选择一个文件夹", out selectedDir, @"C:\Users\Public");

            if (success)
            {
                // 在这里你可以处理用户选择的路径
                BaseDirectory = selectedDir;
                ConfigPathgetorset(2); 

            }
        }

        //读取config保存路径与xml-------------------------------------------------------------------------------------------
        //获取下载路径
        private ConfigManager configManager;
        


        private void Configmanager()
        {
            string configFilePath = Path.Combine("..", "..", "..", "config", "config.xml");
            configManager = new ConfigManager(configFilePath);
        }
        private void ConfigPathgetorset(int select)
        {
            
            

            // 初始化 baseDirectory
            switch (select)
            {
                case 1:
                BaseDirectory = configManager.GetValue("savePath") ?? @"C:\default\path";
                    string autoStartValue = configManager.GetValue("AutoStart");

                    bool.TryParse(autoStartValue, out autotoStart);
                    
                    string autoupdateValue = configManager.GetValue("AutoUpdate");

                    bool.TryParse(autoupdateValue, out autoUpdate);
                    

                    break;
                case 2:
                configManager.SetValue("savePath", BaseDirectory);
                    
                    
                    break;
                case 3:

                    configManager.SetValue("AutoStart", AutotoStart.ToString());
                    configManager.SetValue("AutoUpdate", AutoUpdate.ToString());
                    configManager.Save();
                    
                    break;
            }
            
        }
        private setting setting;
        [RelayCommand]
        private void SavePath()
        {
            ConfigPathgetorset(3);
            CloseSettingWindow();
        }

        private void CloseSettingWindow()
        {
            // 调用关闭窗口的方法
            setting?.Close();
        }
        //退出和隐藏到托盘的选择-----------------------------------------------------------------------------------------------
        [RelayCommand]
        private void ConfirmSelection()
        {
            QuiTeyes = true;
            QuiTeno = false;

            if (IsOption1Selected)
            {
                // 执行选项 1 的操作
                System.Windows.Forms.MessageBox.Show("选项 1 被选中并确认！");
            }
            else if (IsOption2Selected)
            {
                // 执行选项 2 的操作

                QuiTeyes = false;
                QuiTeno = true;

            }
            else
            {
                // 如果没有选择任何选项
                System.Windows.Forms.MessageBox.Show("未选择任何选项！");
            }
        }
        [RelayCommand]
        private void Confirmquite()
        {
            System.Windows.Application.Current.Shutdown();

        }
        [RelayCommand]
        private void Cancelquite()
        {
            CloseWindowAction?.Invoke();
        }

        [RelayCommand]

        //开始和更新
        private void Update()
        {
            IsComponentVisible = false;
            updater.LoadUpdateConfig(xmlPath, baseDirectory);
            StartDownload();

            // 例如：Process.Start("game.exe");
        }
        [RelayCommand]
        private void StartGame()
        {
            System.Windows.Forms.MessageBox.Show("正在启动游戏...", "启动提示");
        }



        //设置窗口开启
        [RelayCommand]
        private void Openset()
        {
            // 创建并显示第二个窗口
            setting = new setting();
            setting.DataContext = this;
            Configmanager();
            ConfigPathgetorset(1);
            setting.Show(); // 显示第二个窗口

        }






        [RelayCommand]
        private void VisitWebsite()
        {
            Process.Start(new ProcessStartInfo("https://your-website.com") { UseShellExecute = true });
        }

        [RelayCommand]
        private void ReportIssue()
        {
            Process.Start(new ProcessStartInfo("https://your-feedback-link.com") { UseShellExecute = true });
        }





        [RelayCommand]

        //开关按钮

        private void Close()
        {
            Closedown closedown = new Closedown(this);

            closedown.Show();
        }

        [RelayCommand]
        private void Minimize()
        {


            // 执行最小化
            System.Windows.Application.Current.MainWindow.WindowState = WindowState.Minimized;



        }
        [RelayCommand]
        private async Task StartDownload()
        {
            IsLoading = true;  // 显示进度条
            ProgressValue = 0; // 初始化进度条值
            await SimulateFileDownload(); // 启动进度条更新
        }
        public async Task SimulateFileDownload()
        {

            while (ProgressValue < 100)
            {
                await Task.Delay(200); // 模拟下载进度
                ProgressValue = DownloadService.process; // 更新进度条

            }
            if (ProgressValue == 100)
            {
                IsLoading = false; // 隐藏控件
                XxXXxxx = true;

            }

        }
        //开机自启动按钮的设置
        [RelayCommand]
        public void Autotostart()
        {
            AutoStart.SetAutoStart(AutoUpdate, "dongdong");
            


        }
    }
}